﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Entropy3D.View
{
    /*
     * Represents a virtual "camera"
     */
    public class Camera
    {
        private Engine engine;

        private Transform transform;
        public Transform Transform
        {
            get { return transform; }
        }

        private Matrix view;
        public Matrix View
        {
            get { return view; }
        }
        
        private Matrix projection;
        public Matrix Projection
        {
            get { return projection; }
        }

        private float fov = 60.0f;
        public float Fov
        {
            get { return fov; }
            set
            {
                fov = MathHelper.Clamp(value, 0.1f, 359.9f);
            }
        }

        private float ortoSize = 10f;
        public float Size
        {
            get { return ortoSize; }
            set { ortoSize = value; }
        }

        private enum Type
        {
            PERSPECTIVE,
            ORTOGRAPHIC
        }

        private Type type = Type.PERSPECTIVE;

        private float nearPlane = 0.01f;
        public float NearPlane
        {
            get { return nearPlane; }
            set
            {
                nearPlane = MathHelper.Clamp(value, 0f, farPlane);
            }
        }
        private float farPlane = 10000f;
        public float FarPlane
        {
            get { return farPlane; }
            set
            {
                farPlane = MathHelper.Clamp(value, nearPlane, float.MaxValue);
            }
        }

        public Camera(Engine e)
        {
            this.engine = e;
            this.transform = new Transform();
            // view = Matrix.CreateLookAt(new Vector3(0f, 0f, 10f), new Vector3(0f, 0f, 0f), Vector3.Up);
            // projection = Matrix.CreatePerspective(8f, 5f, 0.1f, 100f);

            transform.Translate(new Vector3(0f, 0f, -10f));

            UpdateChanges();
        }

        private void UpdateChanges()
        {
            switch (type)
            {
                case Type.PERSPECTIVE:
                    projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(fov), engine.Manager.GraphicsDevice.DisplayMode.AspectRatio, nearPlane, farPlane);
                    break;
                case Type.ORTOGRAPHIC:
                    // NOT YET IMPLEMENTED
                    break;
            }
        }

        public void Update(GameTime gameTime)
        {
            view = transform.TransformMatrix;
        }
    }
}
